Skip to main content

D1

Querying Overview

D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine. It supports a number of database-level statements that allow you to list tables, indexes, and inspect the schema for a given table or index. A summary of the D1 database can be found at https://developers.cloudflare.com/d1/sql-api/sql-statements/

The Qarbine Administrator is responsible for setting various parameters such as the interaction tokens for querying and obtaining vector embeddings.

Date Handling

D1 does not have a storage class set aside for storing dates and/or times. Instead, the built-in date and time functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can choose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions. For more details see the page at https://www.sqlite.org/lang_datefunc.html

This query

SELECT DATE('now') AS current_date

returns a string value

{
"current_datetime": "2025-05-27 01:49:31"
}

This query

SELECT DATETIME('now') AS current_datetime

returns a string value

{
"current_datetime": "2025-05-27 01:49:31"
}

This query

#pragma convertToDate current_date
SELECT DATE('now') AS current_date

returns a Date value

{
"current_date": The Date object for 2025-05-27T00:00:00.000Z
}

This query

#pragma convertToDate current_datetime
SELECT DATETIME('now') AS current_datetime

returns a Date value

{
"current_date": The Date object for 2025-05-27T05:54:55.000Z
}

Boolean Handling

D1 does not have a separate Boolean storage class. Instead, Boolean values are encoded as numbers or text values. The typical integer encodings are 0 (false) and 1 (true).

This query uses a Qarbine pragma to convert common boolean encodings into genuine booleans

#pragma convertToBoolean bool0, bool1, bool2, bool3
SELECT 0 as bool0, 1 as bool1, 'n' as bool2, 'y' as bool3

The results are shown below.

bool0  bool1  bool2  bool3
false true false true

Virtual Queries

Qarbine support several virtual queries as listed in the table below.

Function Description
List databasesList the database names and their UUID values.
Describe database <DB>Provide details on the given database.
Describe databasesProvide details on all databases.
List tablesList the tables in the database.
Describe table <TABLE>Describe the details of the given table.
Describe tablesDescribe all of the database’s tables/

See the “DBA Productivity” section for a D1 document describing their uses and other SQL queries.

Troubleshooting

There are several options to debug the SQL versus answer set expectations.

Use the Cloudflare Dashboard D1 Console

  • The Cloudflare Dashboard provides a built-in D1 SQL Console where you can run queries interactively, inspect tables, and view results directly.
  • This is useful for quickly testing queries, verifying data, and debugging issues outside of your application code.
  • Accessible via your Cloudflare Dashboard under "Storage & Databases &gt; D1 SQL Database." This is the primary online tool for interactive troubleshooting, schema inspection, and query testing.

Use Wrangler CLI for Local and Remote Queries

  • You can use wrangler d1 execute to run SQL queries against your D1 database from your terminal. This is helpful for both troubleshooting and scripting database operations.

Inspect Schema and Metadata

  • Use SQL statements like .schema or PRAGMA table_info(table_name); in the D1 console or via Wrangler to inspect table structures and confirm column types.